package com.buddy.lock.reentrantlock._03;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyService {
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void await(){
        try {
            //必须在condition.await()方法调用前，用lock.lock()获得同步监视器，
            // 否则有 IllegalMonitorStateException监视器出错 异常
            lock.lock();
            System.out.println("await 时间为："+System.currentTimeMillis());
            condition.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void signal(){
        try {
            lock.lock();
            System.out.println("signal 时间为："+System.currentTimeMillis());
            condition.signal();
        }finally {
            lock.unlock();
        }
    }
}
